﻿
using Dapper;
using HBase;
using System.Collections.Generic;
using System;
using System.Data; 

namespace hCommand
{
    //一个任务完成则不允许执行删除任务
    public class WcsDeleteTaskEngine : AbsTaskEngine {
        public long paramId;
        protected CoreTask ct;

        protected CoreTaskParam param;
        //入库完成反馈
        protected override TransRes Execute(IDbConnection conn ,IDbTransaction tran) {
            param = conn.GetById<CoreTaskParam>(paramId ,tran);
            ct = getCoreTask(conn ,tran ,param.taskId);
            String sql1 = @"select * from core_stock where id=@id ";
            AbsCoreStock cs = conn.QueryUnique<AbsCoreStock>(sql1, new { id=ct.stockId }, tran) ;
            if (cs == null) {
                throw new Exception(paramId + "无有效记录");
            }
         
            taskDeleteUpdateStock(conn ,tran ,cs ,param.src);
  
            exec(conn ,tran ,"update Core_Task set Status=-1, Finsh_Time=sgj_get_sys_now where id=@id " ,ct);
            exec(conn ,tran ,"update Core_Task_Param set Status=-1 where task_Id=@id " ,ct);
            UpdataWhLoc(conn ,tran ,param.src , cs.deepDiff);
            UpdataWhLoc(conn ,tran ,param.des ,0);

            return new TransRes();
        }


         public void taskDeleteUpdateStock(IDbConnection Connection, IDbTransaction transaction, AbsCoreStock cs ,int src) {
            long id = cs.id;
          
            int status = cs.status;
            int locId = src;
            if (status == (int)StockStatus.盘库中) {
                return;
            }
            if (!I18nConfig.isLoc(src)) {
                status = (int)StockStatus.库外;
            } else if (I18nConfig.isLoc(src)) {
                status = (int)StockStatus.库存;
            } else {
                throw new Exception("未知任务类型" + id);
            }
            string sql = @"update Core_stock set Status=@status,loc_Id=@locId, change_Time=sgj_get_sys_now where id=@id ";
            GkTransaction.exec(Connection, transaction, sql, new { id, status, locId });
            I18nConfig.deleteTask (Connection, transaction, cs, status);
           

          
        }

      

    }
}
